// stores/permission.js
import { defineStore } from 'pinia'
import { authRoutes } from '@/router/routes'

export const usePermissionStore = defineStore('permission', {
  state: () => ({
    routes: [],
    hasGenerated: localStorage.getItem('routeGenerated') === 'true'
  }),
  
  actions: {
    async generateRoutes(roles) {
      if (this.hasGenerated) return;
      
      const userRoles = roles;
      const filterRoutes = (routes) => 
        routes.filter(route => {
          // 简化后的权限验证逻辑
          const requiredRoles = route.meta?.roles || [];
          return requiredRoles.length === 0 || 
                 requiredRoles.some(r => userRoles.includes(r));
        });

      this.routes = filterRoutes(authRoutes);
      localStorage.setItem('routeGenerated', 'true'); // 持久化状态
    },
    
    setHasGenerated(status) {
      this.hasGenerated = status;
      localStorage.setItem('routeGenerated', Date.now());
    }
  }
});
